package miru.epson.thread;

import miru.epson.common.CommonVal;
import miru.epson.common.HandlerWhat;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.epson.tmcprint.Print;
import com.epson.tmcprint.Status;
import com.epson.tmcprint.TmcException;

public class PrintStatusThread extends Thread{
	Status beforeStatus, currentStatus;
	Print print;
	Handler hand;
	
	public PrintStatusThread(Print print, Handler hand) {
		this.print = print;
		this.hand = hand;
		
		try {
			beforeStatus = print.getPrinterStatus();
			Message msg = new Message();
			msg.obj = beforeStatus.toString();
			msg.what = HandlerWhat.STATUS_PRINT_STATUS;
			hand.sendMessage(msg);
		} catch (TmcException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		while(!CommonVal.isPrintting){
			try {
				currentStatus = print.getPrinterStatus();
			} catch (TmcException e1) {
				e1.printStackTrace();
			}
			
			Log.i("Monitoring", "currentStatus = " + currentStatus + "  beforeStatus = " + beforeStatus);
			
			if(beforeStatus != currentStatus){
				beforeStatus = currentStatus;
				Message msg = new Message();
				msg.obj = currentStatus.toString();
				msg.what = HandlerWhat.STATUS_PRINT_STATUS;
				hand.sendMessage(msg);
			}
			
			try {
				join(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
